package com.scope.customViews;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import com.scope.R;

/**
 * Created by DELL on 10/2/13.
 */
public class LabelButton extends ImageButton {
    public LabelButton(Context context) {
        this(context, null);
    }

    protected LabelButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void initializeViews(Context context) {
        super.initializeViews(context);

        mTextView = new TextView(context);
        mTextView.setText("label");
        mTextView.setTextColor(context.getResources().getColor(R.color.Black));
        mTextView.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.icon_label_v_l));
        addView(mTextView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        setOrientation(CustomView.Orientation.VERTICAL);
    }

    @Override
    protected void initializeAttrs(Context context, AttributeSet attrs) {
        initImageProp();

        super.initializeAttrs(context, attrs);

        TypedArray arr = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.LabelButton,
                0, 0
        );

        try {
            setLabel(arr.getString(R.styleable.LabelButton_label));
//            setLabelSize(arr.getDimensionPixelSize(R.styleable.LabelButton_label_size, R.dimen.icon_label_v_s));
//            setLabelVisibility(arr.getBoolean(R.styleable.LabelButton_label_visible, true));
        } finally {
            arr.recycle();
        }
    }

    protected int mOrientation;

    public void setOrientation(int orientation) {
        mOrientation = orientation;
        positionViews();
    }

    public int getOrientation() {
        return mOrientation;
    }

    private void positionViews() {
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(CENTER_IN_PARENT);
        if (mOrientation == CustomView.Orientation.VERTICAL) {
            lp.addRule(BELOW, mImageView.getId());
            mTextView.setTypeface(Typeface.DEFAULT_BOLD);
            mTextView.setSingleLine(true);
        } else if (mOrientation == CustomView.Orientation.HORIZONTAL) {
            lp.setMargins(getResources().getDimensionPixelSize(R.dimen.icon_margin), 0, 0, 0);
            lp.addRule(RIGHT_OF, mImageView.getId());
            mTextView.setTypeface(Typeface.DEFAULT);
            mTextView.setSingleLine(false);
        } else {
        }
        mTextView.setLayoutParams(lp);
    }

    private void setImageSize(int width, int height) {
        if (mSizeDegree != CustomView.SizeDegree.CUSTOM) {
            return;
        }

        if (width != mImageWidth || height != mImageHeight) {
            if (width != LayoutParams.MATCH_PARENT) {
                mImageWidth = width;
            }
            if (height != LayoutParams.MATCH_PARENT) {
                mImageHeight = height;
            }
            reviseImageSize(width, height);
        }
    }

//    public Point getImageSize() {
//        Point point = new Point();
//        point.set(mImageWidth, mImageHeight);
//        return point;
//    }

    protected void initImageProp() {
        switch (mOrientation) {
            case CustomView.Orientation.VERTICAL:
                mImageLayoutParamRules = new int[]{CENTER_IN_PARENT, ALIGN_PARENT_TOP};
                mImageMarginLeft = 0;
                mImageMarginTop = getResources().getDimensionPixelSize(R.dimen.icon_margin);
                break;
            case CustomView.Orientation.HORIZONTAL:
                mImageLayoutParamRules = new int[]{CENTER_IN_PARENT, ALIGN_PARENT_LEFT};
                mImageMarginTop = 0;
                mImageMarginLeft = getResources().getDimensionPixelSize(R.dimen.icon_margin);
                break;
            default:
                break;
        }
    }

    public CharSequence getLabel() {
        return mTextView.getText();
    }

    public void setLabel(CharSequence label) {
        if (mTextView != null) mTextView.setText(label);
    }

//    public void setLabelSize(float size) {
//        if (mSizeDegree == CustomView.SizeDegree.CUSTOM) {
//            mTextView.setTextSize(size);
//        }
//    }
//
//    public float getLabelSize() {
//        return mTextView.getTextSize();
//    }

    //    protected boolean mLabelVisible = true;
//    public void setLabelVisibility(boolean visible) {
//        if (visible != mLabelVisible) {
//            mLabelVisible = visible;
//
//            if (visible) {
//                setImageSize(mImageWidth, mImageHeight);
//                mTextView.setVisibility(View.VISIBLE);
//            } else {
//                mTextView.setVisibility(View.GONE);
//                reviseImageSize(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//            }
//        }
//    }
//
//    public boolean getLabelVisibility() {
//        return mLabelVisible;
//    }
//
    protected TextView mTextView;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getResources().getDimensionPixelSize(R.dimen.default_width);
        int height = getResources().getDimensionPixelSize(R.dimen.default_height);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode != MeasureSpec.UNSPECIFIED) {
            width = MeasureSpec.getSize(widthMeasureSpec);
        } else {
            widthMode = MeasureSpec.EXACTLY;
        }

        if (heightMode != MeasureSpec.UNSPECIFIED) {
            height = MeasureSpec.getSize(heightMeasureSpec);
        } else {
            heightMode = MeasureSpec.EXACTLY;
        }

        resizeViews(width, height);

        super.onMeasure(
                MeasureSpec.makeMeasureSpec(width, widthMode),
                MeasureSpec.makeMeasureSpec(height, heightMode));
    }

    private void resizeViews(int width, int height) {
        if (mOrientation == CustomView.Orientation.VERTICAL) {
            positionVerticalView(width, height);
        } else if (mOrientation == CustomView.Orientation.HORIZONTAL) {
            positionHorizontalView(width, height);
        } else {
        }
    }

    private float mImageShare = 0.7f;

    public float getImageShare() {
        return mImageShare;
    }

    public void setImageShare(float share) {
        if (share > 0.7f) {
            share = 0.7f;
        } else if (share < 0.3f) {
            share = 0.3f;
        }
        mImageShare = share;
    }

    private boolean mImageSquare = true;

    public boolean ifImageSquare() {
        return mImageSquare;
    }

    public void setSquareImage(boolean square) {
        mImageSquare = square;
        resizeViews(getWidth(), getHeight());
    }

    private void positionVerticalView(int width, int height) {
        int imgHeight = (int) (mImageShare * height);
        if (mImageSquare) {
            imgHeight = Math.min(imgHeight, width);
            setImageSize(imgHeight, imgHeight);
        } else {
            setImageSize(width, imgHeight);
        }

        mTextView.setTextSize(0.75f * (0.9f - mImageShare) * height);
    }

    private void positionHorizontalView(int width, int height) {
        int imgWidth = (int) (mImageShare * width);
        if (mImageSquare) {
            imgWidth = Math.min(imgWidth, height);
            setImageSize(imgWidth, imgWidth);
        } else {
            setImageSize(imgWidth, height);
        }

        mTextView.setTextSize(0.75f * 0.25f * height);
    }

    protected int mSizeDegree = CustomView.SizeDegree.CUSTOM;

//    public void setSizeDegree(int sizeDegree) {
//        if (mSizeDegree == sizeDegree) {
//            return;
//        }
//
//        mSizeDegree = sizeDegree;
//
//        switch (mOrientation) {
//            case CustomView.Orientation.VERTICAL:
//                setVerticalSizeDegree();
//                break;
//            case CustomView.Orientation.HORIZONTAL:
//                setHorizontalSizeDegree();
//                break;
//            default:
//                break;
//        }
//
//        setAccent(mAccent);
//    }
//
//    private void setVerticalSizeDegree() {
//        switch(mSizeDegree) {
//            case CustomView.SizeDegree.SMALL:
//                setImageSize(
//                        getResources().getDimensionPixelSize(R.dimen.icon_width_v_s),
//                        getResources().getDimensionPixelSize(R.dimen.icon_height_v_s)
//                );
//                setLabelSize(getResources().getDimensionPixelSize(R.dimen.icon_label_v_s));
//                break;
//            case CustomView.SizeDegree.LARGE:
//                setImageSize(
//                        getResources().getDimensionPixelSize(R.dimen.icon_width_v_l),
//                        getResources().getDimensionPixelSize(R.dimen.icon_height_v_l)
//                );
//                setLabelSize(getResources().getDimensionPixelSize(R.dimen.icon_label_v_l));
//                break;
//            case CustomView.SizeDegree.MEDIUM:
//                setImageSize(
//                        getResources().getDimensionPixelSize(R.dimen.icon_width_v_m),
//                        getResources().getDimensionPixelSize(R.dimen.icon_height_v_m)
//                );
//                setLabelSize(getResources().getDimensionPixelSize(R.dimen.icon_label_v_m));
//                break;
//            default:
//                mSizeDegree = CustomView.SizeDegree.CUSTOM;
//                break;
//        }
//    }
//
//    private void setHorizontalSizeDegree() {
//        switch(mSizeDegree) {
//            case CustomView.SizeDegree.SMALL:
//                setImageSize(
//                        getResources().getDimensionPixelSize(R.dimen.icon_width_h_s),
//                        getResources().getDimensionPixelSize(R.dimen.icon_height_h_s)
//                );
//                setLabelSize(getResources().getDimensionPixelSize(R.dimen.icon_label_h_s));
//                break;
//            case CustomView.SizeDegree.MEDIUM:
//                setImageSize(
//                        getResources().getDimensionPixelSize(R.dimen.icon_width_h_m),
//                        getResources().getDimensionPixelSize(R.dimen.icon_height_h_m)
//                );
//                setLabelSize(getResources().getDimensionPixelSize(R.dimen.icon_label_h_m));
//                break;
//            case CustomView.SizeDegree.LARGE:
//                setImageSize(
//                        getResources().getDimensionPixelSize(R.dimen.icon_width_h_l),
//                        getResources().getDimensionPixelSize(R.dimen.icon_height_h_l)
//                );
//                setLabelSize(getResources().getDimensionPixelSize(R.dimen.icon_label_h_l));
//                break;
//            case CustomView.SizeDegree.EXTRA_LARGE:
//                setImageSize(
//                        getResources().getDimensionPixelSize(R.dimen.icon_width_h_xl),
//                        getResources().getDimensionPixelSize(R.dimen.icon_height_h_xl)
//                );
//                setLabelSize(getResources().getDimensionPixelSize(R.dimen.icon_label_h_xl));
//                break;
//            default:
//                mSizeDegree = CustomView.SizeDegree.CUSTOM;
//                break;
//        }
//    }
}